#include "defines.h"
#include "sign_in_view.h"
#include "ui_sign_in_view.h"
#include <QLineEdit>

sign_in_view::sign_in_view(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::sign_in_view)
{
    setMainWindowTitle("Sign In");
    ui->setupUi(this);

#ifndef DEBUG
    ui->GOTODEBUG->setVisible(false);
#endif

    ui->password->setEchoMode(QLineEdit::Password);
    ui->username->setText(g_username);
    ui->username->setFocus();

    ui->comboBox->setCurrentIndex(g_currentSession);

    signInErrorLabelHeight = ui->signinErrorLabel->height();
    ui->signinErrorLabel->hide();
    ui->groupBox->resize(ui->groupBox->width(),
                         ui->groupBox->height() - signInErrorLabelHeight);
}

sign_in_view::~sign_in_view()
{
    delete ui;
}

void sign_in_view::on_GOTODEBUG_clicked()
{
    emit(changeScene(DEBUG_VIEW));
}

void sign_in_view::on_comboBox_currentIndexChanged(int index)
{
    g_currentSession = (SessionEnum)index;
}


void sign_in_view::on_signinbutton_clicked()
{
    bool userempty = ui->username->text().isEmpty();
    bool passempty = ui->password->text().isEmpty();

    if (passempty || userempty)
    {
        if(ui->signinErrorLabel->isHidden()){
            ui->signinErrorLabel->show();
            //ui->groupBox->resize(ui->groupBox->width(),
            //                     ui->groupBox->height() + signInErrorLabelHeight);
        }
        if (passempty)
        {
            ui->signinErrorLabel->setText("Please input password.");
            ui->password->setFocus();
        }
        if (userempty)
        {
            ui->signinErrorLabel->setText("Please input username.");
            ui->username->setFocus();
        }
    }
    else
    {
        if (ui->checkBox->isChecked()) {
            g_username = ui->username->text();
        }

        if (g_currentSession == PROCTOR_SESSION)
        {
            emit (changeScene(STUDENT_LIST_VIEW));
        }
        else
        {
            emit (changeScene(TEST_LIST_VIEW));
        }
    }
}

void sign_in_view::on_password_returnPressed()
{
    on_signinbutton_clicked();
}

void sign_in_view::on_username_returnPressed()
{
    on_signinbutton_clicked();
}
